package com.knowvoyage.knowvoyageserver.service.Impl;

import com.knowvoyage.knowvoyageserver.mapper.LearnMapper;
import com.knowvoyage.knowvoyageserver.pojo.dto.LearnDTO;
import com.knowvoyage.knowvoyageserver.pojo.entity.Learn;
import com.knowvoyage.knowvoyageserver.pojo.vo.Result;
import com.knowvoyage.knowvoyageserver.service.LearnService;
import com.knowvoyage.knowvoyageserver.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class LearnServiceImpl implements LearnService {
    private final LearnMapper learnMapper;
    private final JwtUtil jwtUtil;
    @Transactional
    public Result<Boolean> recordLearning(LearnDTO learnDTO, String token) {
        // 1. 身份验证（内聚到Service层）
        token = token.substring(7);

        // 2. 解析Token（原逻辑不变）
        Claims claims = jwtUtil.parseToken(token);
        Long userId = claims.get("id", Long.class);

        // 2. 业务对象转换
        Learn learn = new Learn();
        BeanUtils.copyProperties(learnDTO, learn);
        learn.setUserId(userId);
        LocalDateTime endTime = LocalDateTime.now(); //结束时间=当前时间
        LocalDateTime startTime = endTime.minusMinutes(learnDTO.getDuration());
        learn.setStartTime(startTime);

        learnMapper.insert(learn);
        return Result.success(true);
    }
    @Override
    public Result<Boolean> insert(Learn learn) {
        Long learnId = learn.getLearnId();
        log.info("插入学习记录：{}", learnId);
        if (learnId == null) {
            return Result.error(400,"id为空");
        }
        if(learnMapper.selectById(learnId) != null) {
            return Result.error(400,"学习记录已存在");
        }
        learnMapper.insert(learn);
        return Result.success(true);
    }

    @Override
    public Result<Learn> selectById(Long learnId) {
        if (learnId == null) {
            return Result.error(400, "id为空");
        }
        return Result.success(learnMapper.selectById(learnId));
    }

    @Override
    public Result<List<Learn>> selectByUserId(Long userId) {
        return Result.success(learnMapper.selectByUserId(userId));
    }

    @Override
    public Result<Boolean> delete(Long learnId) {
        learnMapper.delete(learnId);
        return Result.success(true);
    }

    @Override
    public Result<Boolean> update(Learn learn) {
        Long learnId = learn.getLearnId();
        if (learnId == null) {
            return Result.error(400, "id为空");
        }
        if (learnMapper.selectById(learnId) == null) {
            return Result.error(400, "学习记录不存在");
        }
        learnMapper.update(learn);
        return null;
    }

    @Override
    public Result<List<Learn>> selectAll() {
        return Result.success(learnMapper.selectAll());
    }

    @Override
    public List<Learn> selectByTimeRange(Long userId, LocalDateTime startTime, LocalDateTime endTime) {
        return learnMapper.selectByUserIdAndTimeRange(userId, startTime, endTime);
    }

    @Override
    public List<Learn> selectTodayRecords(Long userId) {
        return learnMapper.selectTodayByUserId(userId);
    }

    @Override
    public List<Learn> selectWeekRecords(Long userId) {
        return learnMapper.selectThisWeekByUserId(userId);
    }
}
